package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imufe.chemicalenterprisedangerwarning.entity.po.ProjectChemicalPO;
import com.imufe.chemicalenterprisedangerwarning.entity.po.ProjectPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.ProjectChemicalQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ChemicalDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ProjectChemicalDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.mapper.ProjectChemicalMapper;
import com.imufe.chemicalenterprisedangerwarning.service.ChemicalService;
import com.imufe.chemicalenterprisedangerwarning.service.ProjectChemicalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.ListUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 项目化学品表 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-26
 */
@Log4j2
@Service
public class ProjectChemicalServiceImpl extends ServiceImpl<ProjectChemicalMapper, ProjectChemicalPO> implements ProjectChemicalService {

    @Autowired
    ProjectChemicalMapper mapper;

    @Autowired
    ChemicalService chemicalService;

    @Override
    public ProjectChemicalDetailedVO detailed(Long id) {
        ProjectChemicalPO po = mapper.selectById(id);
        ProjectChemicalDetailedVO detailedVO = BeanUtils.copyPOJO(po, ProjectChemicalDetailedVO.class);
        ChemicalDetailedVO chemicalDetailedVO = chemicalService.detailed(po.getChemicalId());
        detailedVO.setChemical(chemicalDetailedVO);
        log.info(detailedVO);
        return detailedVO;
    }

    @Override
    public List<ProjectChemicalDetailedVO> chemicalList(Long projectId) {
        QueryWrapper<ProjectChemicalPO> qw = new QueryWrapper<>();
        qw.eq("project_id", projectId);
        List<ProjectChemicalPO> pos = mapper.selectList(qw);
        return pos.stream()
                .map(po -> this.detailed(po.getId()))
                .toList();
    }

    @Override
    public ProjectChemicalDetailedVO create(ProjectChemicalQuery query) {
        ProjectChemicalPO po = BeanUtils.copyPOJO(query, ProjectChemicalPO.class);
        log.info(po);
        mapper.insert(po);
        return this.detailed(po.getId());
    }

    @Override
    public ProjectChemicalDetailedVO update(ProjectChemicalQuery query) {
        ProjectChemicalPO po = BeanUtils.copyPOJO(query, ProjectChemicalPO.class);
        log.info(po);
        mapper.updateById(po);
        return this.detailed(po.getId());
    }
}
